package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.ChanPinFenLei;
import com.ysd.util.DBManager;

public class ChanPinFenLeiDaoImpl {
	public List<ChanPinFenLei> getChanPinFenLeiList(){
		List<ChanPinFenLei> cpList=new ArrayList<ChanPinFenLei>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM chanpinfenlei");
			while(rs.next()) {
				int cPFenLeiId=rs.getInt("cPFenLeiId");
				String cPName=rs.getString("cPName");
				ChanPinFenLei cpfl=new ChanPinFenLei(cPFenLeiId, cPName);
				cpList.add(cpfl);
			}
			return cpList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	
	public List<ChanPinFenLei> getChanPinFLList(int cpFlId){
		List<ChanPinFenLei> cpList=new ArrayList<ChanPinFenLei>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM chanpinxinxi WHERE fenLeiID="+cpFlId);
			while(rs.next()) {
				int cPFenLeiId=rs.getInt("fenLeiID");
				String cPName=rs.getString("cPName");
				ChanPinFenLei cpfl=new ChanPinFenLei(cPFenLeiId, cPName);
				cpList.add(cpfl);
			}
			return cpList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			DBManager.closeAll(conn, st, rs);
		}
	}
	
	/**
	 * 根据产品分类id删除产品分类
	 * @param cpFLId 产品分类id
	 * @return
	 */
	public int delChanPinFLId(int cpFLId) {
		return DBManager.executeUpdate("DELETE FROM chanpinfenlei WHERE cPFenLeiId="+cpFLId);
	}
	/**
	 * 批量删除
	 * @param cpFLId
	 * @return
	 */
	public int delChanPinFLIds(String cpFLIds) {
		return DBManager.executeUpdate("DELETE FROM chanpinfenlei WHERE cPFenLeiId in("+cpFLIds+")");
	}
	/**
	 * 添加分类
	 * @param cpM
	 * @return
	 */
	public int addCPFenLei(ChanPinFenLei cpM) {
		return DBManager.executeUpdate("INSERT INTO chanpinfenlei VALUES(null,'"+cpM.getcPName()+"')");
	}
	
	/**
	 * 根据名称查询产品分类
	 * @param cpM
	 * @return
	 */
	public List<ChanPinFenLei> getChanPinFenLei(String cpM){
		List<ChanPinFenLei> cpList=new ArrayList<ChanPinFenLei>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * FROM chanpinfenlei WHERE cPName='"+cpM+"'");
			while(rs.next()) {
				int cpid=rs.getInt("cPFenLeiId");
				String cpname=rs.getString("cPName");
				ChanPinFenLei cpfl=new ChanPinFenLei(cpid, cpname);
				cpList.add(cpfl);
			}
			return cpList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	//编辑产品信息
	public ChanPinFenLei getcPId(int cpId) {
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		ChanPinFenLei cpfl=null;
		try {
			st=conn.createStatement();
			rs=st.executeQuery("SELECT * from chanpinfenlei WHERE cPFenLeiId="+cpId);
			while(rs.next()) {
				int cpid=rs.getInt("cPFenLeiId");
				String cPName=rs.getString("cPName");
				cpfl=new ChanPinFenLei(cpid, cPName);
			}
			return cpfl;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBManager.closeAll(conn, st, rs);
		}
		
	}
	
	public int updateChanPinFenLei(ChanPinFenLei fl) {
		String sql="UPDATE chanpinfenlei SET cPName='"+fl.getcPName()+"' WHERE cPFenLeiId="+fl.getcPFenLeiId();
		System.out.println(sql);
		return DBManager.executeUpdate(sql);
	}
	
	/**
	 * 分页查询
	 * @return
	 */
	public List<ChanPinFenLei> getChanPinFenLeiListByPage(int page,int size){
		List<ChanPinFenLei> cpList=new ArrayList<ChanPinFenLei>();
		Connection conn=DBManager.getConnection();
		Statement st=null;
		ResultSet rs=null;
		
		try {
			st=conn.createStatement();
			String sql = "select * from chanpinfenlei limit "+(page-1)*size+","+size;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int cPFenLeiId=rs.getInt("cPFenLeiId");
				String cPName=rs.getString("cPName");
				ChanPinFenLei cpfl=new ChanPinFenLei(cPFenLeiId, cPName);
				cpList.add(cpfl);
			}
			return cpList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally{
			DBManager.closeAll(conn, st, rs);
		}
	}
}
